﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Windows.Forms;
using Soapi.Collections;
using Soapi.Notify2.Lib;


namespace Soapi.Notify2
{
    static class Program
    {

        public static ApiContext Context;
        public static UserSettings Settings;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Soapi.Net.RequestThrottle.Instance.ThrottleWindowTime = new TimeSpan(0, 0, 8);
            Context = new ApiContext("GFoeNudbf0qc79SpEI56ZA");
            try
            {
                Context.Initialize(false);
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error initializing the API context.\r\n" + ex.Message + "\r\n\r\nPlease try again later or report this to the developer.");
                Application.Exit();
                return;
            }

            Settings = new UserSettings();

            Context.Sites.ForEach(s =>
                {
                    var site = Settings.Sites.Find(s.ApiEndpoint);
                    if (site == null)
                    {
                        site = new SiteListItem(s.Name, s.ApiEndpoint, false);
                        Settings.Sites.Add(site);
                    }
                    site.Site = s;
                });
            Settings.Save();

            Application.Run(new MainForm());
            Settings.Save();
        }
    }




}
